// 全局统一拦截器
import {axios} from "para-lib";
import {message} from "antd";
import {store} from "../store";
import {intl} from "../locale";
import {authLogOutActions} from "../store/actions/authAction";

const interceptor = (Message: any) => {
    axios.defaults.baseURL = "/api/";
    // 请求拦截器
    axios.interceptors.request.use(
        function (config) {
            //国际化请求头
            config.headers.language = window.localStorage.getItem("language") || config.headers["Accept-Language"] || "zh";
            // try {
            //     /* 设置请求头header */
            //     let {auth} = JSON.parse(localStorage.getItem('plcconsole') || '{}') || {}
            //     let {token} = JSON.parse(auth || '{}') || {}
            //     if (token) {
            //         //将token放到请求头发送给服务器,将token key放在请求头中
            //         config.headers.Authorization = "Bearer " + token;
            //     }
            // } catch (err) {
            //
            // }
            let {
                auth: {token}
            } = store.getState();
            if (token) {
                //将token放到请求头发送给服务器,将token key放在请求头中
                config.headers.Authorization = "Bearer " + token;
            }
            return config;
        },
        function (error) {
            return Promise.reject(error);
        }
    );

    // 响应拦截器
    axios.interceptors.response.use(
        (response: any) => {
            const {data: resData, config}: any = response;
            if (config.global === true) return response;
            if (resData) {
                const {code, message: msg} = resData;
                console.log("code", code);
                if (Number(code) !== 200 && Number(code) !== 0) {
                    if (Number(code) === 401) return toLogin();
                    if (config.glabal !== "msg") Message.error(msg);
                    if (Number(code) === 904) return toLogin();
                    if (Number(code) === 601) return toLogin();
                    return Promise.reject({code, msg});
                }
            }
            return response;
        },
        function (error) {
            const {response, config} = error;
            if (!response && config.global !== true) return Message.error(error.message && intl("networkError"));
            const status = String(response.status);
            if (status === "401") {
                return toLogin();
            } else {
                let msg: string = "";
                switch (status) {
                    case "403":
                        msg = intl("403");
                        window.location.hash = "/403";
                        break;
                    case "404":
                        msg = intl("404");
                        break;
                    case "500":
                        msg = intl("500");
                        break;
                    default:
                        msg = intl("unknown");
                        break;
                }
                Message.error(msg);
            }
        }
    );

    // 跳转登陆页
    const toLogin = async () => {
        await store.dispatch(authLogOutActions(store.dispatch));
    };
};
export default interceptor;
